package com.ruoyi.system.service.impl;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.TStudent;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.TStudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TScoreMapper;
import com.ruoyi.system.domain.TScore;
import com.ruoyi.system.service.ITScoreService;

/**
 * 成绩Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-27
 */
@Service
public class TScoreServiceImpl implements ITScoreService {
    @Autowired
    private TScoreMapper tScoreMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private TStudentMapper tStudentMapper;

    /**
     * 查询成绩
     *
     * @param id 成绩主键
     * @return 成绩
     */
    @Override
    public TScore selectTScoreById(Long id) {
        return tScoreMapper.selectTScoreById(id);
    }

    /**
     * 查询成绩列表
     *
     * @param tScore 成绩
     * @return 成绩
     */
    @Override
    public List<TScore> selectTScoreList(TScore tScore) {
        SysUser user = sysUserMapper.selectUserById(SecurityUtils.getUserId());
        if(user.getRoles() != null){
            boolean contains = user.getRoles().stream().map(SysRole::getRoleId)
                    .collect(Collectors.toList()).contains(100L);
            if(contains){
                String userName = user.getUserName();
                TStudent tStudent = tStudentMapper.selectTStudentByNumber(userName);
                Long id = tStudent.getId();
                tScore.setStudentId(id);
            }
        }
        return tScoreMapper.selectTScoreList(tScore);
    }

    /**
     * 新增成绩
     *
     * @param tScore 成绩
     * @return 结果
     */
    @Override
    public int insertTScore(TScore tScore) {
        Double score = tScore.getScore();
            SysDictData sysDictData = sysDictDataMapper.selectDictDataById(tScore.getProject());
        String dictValue = sysDictData.getDictValue();
        String[] split = dictValue.split("-");
        try {
            if(split[1].equals("0")){
                double v = calculateScore(Double.valueOf(split[0]), 100, score);
                DecimalFormat df = new DecimalFormat("#.##");
                if(v<=100){
                    tScore.setDivide(Double.valueOf(df.format(v)));
                }else {
                    tScore.setDivide(100.0);
                }
            }else {
                double v = calculateScore(score, 100, Double.valueOf(split[0]));
                DecimalFormat df = new DecimalFormat("#.##");
                if(v<=100){
                    tScore.setDivide(Double.valueOf(df.format(v)));
                }else {
                    tScore.setDivide(100.0);
                }
            }
        } catch (NumberFormatException e) {
            throw new GlobalException("设置格式错误"+dictValue);
        }
        return tScoreMapper.insertTScore(tScore);
    }

    public static void main(String[] args) {
        System.out.println(calculateScore( 200, 100, 150) );
    }

    public static double calculateScore( double perfectTime, int perfectScore, double actualTime) {
        // 参数合法性校验
        if (perfectTime <= 0 || perfectScore <= 0 || actualTime <= 0) {
            throw new IllegalArgumentException("参数必须为正数");
        }
        // 计算得分公式：(满分时间 / 实际时间) * 满分分值
        double score = (perfectTime / actualTime) * perfectScore;
        // 四舍五入取整并确保不超过满分
        return score;
    }



    /**
     * 修改成绩
     *
     * @param tScore 成绩
     * @return 结果
     */
    @Override
    public int updateTScore(TScore tScore) {
        return tScoreMapper.updateTScore(tScore);
    }

    /**
     * 批量删除成绩
     *
     * @param ids 需要删除的成绩主键
     * @return 结果
     */
    @Override
    public int deleteTScoreByIds(Long[] ids) {
        return tScoreMapper.deleteTScoreByIds(ids);
    }

    /**
     * 删除成绩信息
     *
     * @param id 成绩主键
     * @return 结果
     */
    @Override
    public int deleteTScoreById(Long id) {
        return tScoreMapper.deleteTScoreById(id);
    }
}
